package com.tanhua.recommend.msg;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tanhua.dubbo.pojo.Publish;
import com.tanhua.recommend.pojo.RecommendQuanZi;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.bson.types.ObjectId;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;

@Component
@RocketMQMessageListener(topic = "tanhua-quanzi",
        consumerGroup = "tanhua-quanzi-consumer")
public class QuanZiMsgConsumer implements RocketMQListener<String> {

    private static final ObjectMapper MAPPER = new ObjectMapper();
    private static final Logger LOGGER = LoggerFactory.getLogger(QuanZiMsgConsumer.class);

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void onMessage(String message) {
        try {
            JsonNode jsonNode = MAPPER.readTree(message);
            long userId = jsonNode.get("userId").asLong();
            long pid = jsonNode.get("pid").asLong();
            String publishId = jsonNode.get("publishId").asText();
            int type = jsonNode.get("type").asInt();

            RecommendQuanZi recommendQuanZi = new RecommendQuanZi();
            recommendQuanZi.setId(ObjectId.get());
            recommendQuanZi.setPid(pid);
            recommendQuanZi.setUserId(userId);
            recommendQuanZi.setDate(System.currentTimeMillis());

            // 对操作的类型进行判断
            /*
            1-发动态，2-浏览动态， 3-点赞， 4-喜欢， 5-评论，6-取消点赞，7-取消喜欢
            - 浏览 +1
            - 点赞 +5
            - 喜欢 +8
            - 评论 + 10
            - 文字长度：50以内1分，50~100之间2分，100以上3分
            - 图片个数：每个图片一分
             */
            switch (type) {
                case 1:
                    int score = 0;
                    Publish publish = mongoTemplate.findById(publishId, Publish.class);
                    if (publish != null) {
                        int length = publish.getText().length();
                        if (length > 0 && length <= 50) {
                            score = 1;
                        } else if (length > 50 && length <= 100) {
                            score = 2;
                        } else {
                            score = 3;
                        }
                        int size = publish.getMedias().size();
                        score += size;
                    }
                    recommendQuanZi.setScore(Double.valueOf(score));
                    break;
                case 2:
                    recommendQuanZi.setScore(1D);
                    break;
                case 3:
                    recommendQuanZi.setScore(5D);
                    break;
                case 4:
                    recommendQuanZi.setScore(8D);
                    break;
                case 5:
                    recommendQuanZi.setScore(10D);
                    break;
                case 6:
                    recommendQuanZi.setScore(-5D);
                    break;
                case 7:
                    recommendQuanZi.setScore(-8D);
                    break;
                default:
                    recommendQuanZi.setScore(0D);
                    break;
            }

            String collectionName = "recommend_quanzi_" + new DateTime().toString("yyyyMMdd");
            mongoTemplate.save(recommendQuanZi,collectionName);

        } catch (Exception e) {
            LOGGER.error("处理消息失败！" + message, e);
        }

    }
}
